
#include "string.h"

/*******************************************************************************
 **  Name: component.cpp                                                      **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                      **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
// Modified $DateTime$ by $Author$
#define _component_cpp_ID    "$Id$"

#include "component.h"

uint32 Component::nextID = 0;

void
Component::AddUpgrade(ComponentUpgrade * u)
{
    if (upgrades) {
        u->next = upgrades;
        upgrades = u;
    } else
        upgrades = u;
}
void
Component::AddQuality(ComponentQuality * q)
{
    if (qualities) {
        q->next = qualities;
    }
    qualities = q;
}


Component::Component(const char *n,ComponentType t,uint32 i)
{
    name = new char[strlen(n)+1];
    strcpy(name,n);
    type = t;
    id = i;
    ver = 1;
    upgrades = NULL;
    qualities = NULL;
    cost = 0;
    hardPoints = 0;
    upgradeSlots[0] = upgradeSlots[1] = 0;
    weight = 0;
}

Component::Component()
{
    name = NULL;
    type = ComponentType::max;
    id   = NextID();
    ver  = 0;
    upgrades = NULL;
    qualities = NULL;
    cost = 0;
    hardPoints = 0;
    upgradeSlots[0] = upgradeSlots[1] = 0;
    weight = 0;
}

Component::Component(const Component& orig)
{
    if (name) delete [] name;
    name = new char[strlen(orig.name)+1];
    strcpy(name,orig.name);
    type = orig.type;
    id = orig.id;
    ver = orig.ver;
    upgrades = orig.upgrades;   // todo deep copy
    qualities = orig.qualities;
    cost = orig.cost;
    hardPoints = orig.hardPoints;
    weight = orig.weight;
    upgradeSlots[0] = orig.upgradeSlots[0];
    upgradeSlots[1] = orig.upgradeSlots[1];
}

Component::~Component()
{
    if (name) delete [] name;
    while (upgrades) {
        ComponentUpgrade * del = upgrades;
        upgrades = upgrades->next;
        delete del;
    }
    while (qualities) {
        ComponentQuality * del = qualities;
        qualities = qualities->next;
        delete del;
    }
}

